home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / Documents / NeXTAnswers / objc.780 < prev    next >
Text File  |  1992-02-06  |  2KB  |  71 lines

  1. {\rtf0\ansi{\fonttbl\f0\fnil Times-Roman;\f2\fmodern Courier;\f1\fswiss Helvetica;}
  2. \paperw11040
  3. \paperh5640
  4. \margl120
  5. \margr1000
  6. {\colortbl\red0\green0\blue0;}
  7. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\b0\i0\ul0\fs28 C++ enum declaration bug workaround\
  8. \
  9. Q:  When I try to compile my C++ program which contains this header file:\
  10. \
  11.  
  12. \pard\tx1140\tx2300\tx3440\tx4600\tx5760\tx6900\tx8060\tx9200\tx10360\tx11520\f2\fs24\fc0     typedef enum \{\
  13.     aaa, \
  14.     bbb,\
  15.     ccc \
  16.     \} Type;\
  17.     \
  18.     class myClass \{\
  19.     private:\
  20.     Type myType; \
  21.     public:\
  22.     void setType(Type newType);\
  23.     \};\
  24.  
  25. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\fs28 \
  26. I get the link error:\
  27. \
  28.  
  29. \f2\fs24 /bin/ld:Undefined symbols:
  30. \pard\tx1140\tx2300\tx3440\tx4600\tx5760\tx6900\tx8060\tx9200\tx10360\tx11520\fc0 _setType__7myClass3$_1
  31. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600 \
  32.  
  33. \f0\fs28 \
  34. and the compilation stops.  The same program would compile fine on a non-NeXT c++ compiler.  What's the problem?\
  35. \
  36. A:  
  37. \fc0 There is indeed a bug in the current C++ compiler, but the workaround is easy.\
  38. \
  39. The problem is with the declaration:\
  40. \
  41.  
  42. \f2\fs24     typedef enum \{\
  43.     aaa,\
  44.     bbb,\
  45.     ccc\
  46.     \} Type;
  47. \f0\fs28 \
  48. \
  49. This causes problems with C++'s name mangling, since this enum has no tag.  The current compiler does not know how to encode this type, so it simply uses an integer for the number of the unencodable type.  There is no reason to think that this will match the encoding used in some other file which may have had a different number of unencodable types or the same types but in a different order.  This is indeed what was happening in your case, and explains why the link failed.\
  50. \
  51. You should just use this declaration instead:\
  52. \
  53.  
  54. \f2\fs24     enum Type \{\
  55.     aaa,\
  56.     bbb,\
  57.     ccc\
  58.     \};\
  59.  
  60. \f0\fs28 \
  61. In C++, a typedef is automatically published for each struct, union, or enum with the same name as the tag, so you can continue to use "Type" rather than "enum Type".\
  62. \
  63. This bug will be fixed in a future release of the C++ compiler.\
  64. \
  65. QA780\
  66. \
  67. Not Valid for 1.0\
  68. Valid for 2.0\
  69. \
  70.  
  71.